package com.esri.android.map.popup;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.esri.android.map.ags.ArcGISPopupInfo;
import com.esri.android.map.popup.ArcGISPopupStyle;
import com.esri.core.map.CodedValueDomain;
import com.esri.core.map.Domain;
import com.esri.core.map.FeatureType;
import com.esri.core.map.Field;
import com.esri.core.map.RangeDomain;
import com.esri.core.map.popup.PopupFieldInfo;
import com.esri.core.map.popup.PopupInfo;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/* loaded from: classes.dex */
public abstract class ArcGISAttributesAdapter implements Adapter {
    protected static final int DEFAULT_CODED_VALUE_LAYOUT = -2;
    protected static final int DEFAULT_DATE_LAYOUT = -4;
    protected static final int DEFAULT_EDIT_TEXT_LAYOUT = -5;
    protected static final int DEFAULT_RANGE_VALUE_LAYOUT = -3;
    protected static final int FIELD_LABEL_ID = 1;
    protected static final int FIELD_VALUE_ID = 2;
    private ArrayList<String> a;
    private int b = 48;
    private final DataSetObservable c = new DataSetObservable();
    protected Map<String, Object> mAttributes;
    protected LayoutResource mCodedValueResource;
    protected Context mContext;
    protected int mDatePickerResourceId;
    protected LayoutResource mDateResource;
    protected LayoutResource mEditTextResource;
    protected FeatureType mFeatureType;
    protected Popup mPopup;
    protected LayoutResource mRangeValueResource;
    protected ArcGISValueFormat mValueFormat;

    /* JADX INFO: Access modifiers changed from: protected */
    /* loaded from: classes.dex */
    public enum FIELD_TYPE {
        CODED_VALUE_DOMAIN,
        RANGE_DOMAIN,
        DATE,
        TEXT,
        UNSUPPORTED
    }

    public boolean areAllItemsEnabled() {
        return true;
    }

    protected abstract ViewGroup createDefaultLayout(FIELD_TYPE field_type);

    @Override // android.widget.Adapter
    public long getItemId(int i) {
        return i;
    }

    @Override // android.widget.Adapter
    public int getItemViewType(int i) {
        return 0;
    }

    @Override // android.widget.Adapter
    public int getViewTypeCount() {
        return 1;
    }

    @Override // android.widget.Adapter
    public boolean hasStableIds() {
        return true;
    }

    public boolean isEnabled(int i) {
        return true;
    }

    public abstract void refresh();

    /* loaded from: classes.dex */
    protected class LayoutResource {
        public int mId;
        public int mLabelViewId;
        public int mValueViewId;

        public LayoutResource(int i, int i2, int i3) {
            this.mId = i;
            this.mLabelViewId = i2;
            this.mValueViewId = i3;
        }

        public ViewGroup createLayout() {
            int i = this.mId;
            if (i > 0) {
                return (ViewGroup) ((Activity) ArcGISAttributesAdapter.this.mContext).getLayoutInflater().inflate(this.mId, (ViewGroup) null);
            }
            if (i != -4) {
                if (i != -3) {
                    if (i == -2) {
                        return ArcGISAttributesAdapter.this.createDefaultLayout(FIELD_TYPE.CODED_VALUE_DOMAIN);
                    }
                    return ArcGISAttributesAdapter.this.createDefaultLayout(FIELD_TYPE.TEXT);
                }
                return ArcGISAttributesAdapter.this.createDefaultLayout(FIELD_TYPE.RANGE_DOMAIN);
            }
            return ArcGISAttributesAdapter.this.createDefaultLayout(FIELD_TYPE.DATE);
        }
    }

    /* renamed from: com.esri.android.map.popup.ArcGISAttributesAdapter$1  reason: invalid class name */
    /* loaded from: classes.dex */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[FIELD_TYPE.values().length];
            a = iArr;
            try {
                iArr[FIELD_TYPE.CODED_VALUE_DOMAIN.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a[FIELD_TYPE.DATE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                a[FIELD_TYPE.RANGE_DOMAIN.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                a[FIELD_TYPE.TEXT.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public LayoutResource getLayoutResource(FIELD_TYPE field_type) {
        int i = AnonymousClass1.a[field_type.ordinal()];
        if (i == 1) {
            if (this.mCodedValueResource == null) {
                this.mCodedValueResource = new LayoutResource(-2, 1, 2);
            }
            return this.mCodedValueResource;
        } else if (i == 2) {
            if (this.mDateResource == null) {
                this.mDateResource = new LayoutResource(-4, 1, 2);
            }
            return this.mDateResource;
        } else if (i == 3) {
            if (this.mRangeValueResource == null) {
                this.mRangeValueResource = new LayoutResource(-3, 1, 2);
            }
            return this.mRangeValueResource;
        } else {
            if (this.mEditTextResource == null) {
                this.mEditTextResource = new LayoutResource(-5, 1, 2);
            }
            return this.mEditTextResource;
        }
    }

    public void setCodedValueLayoutResourceId(int i, int i2, int i3) {
        this.mCodedValueResource = new LayoutResource(i, i2, i3);
    }

    public void setRangeValueLayoutResourceId(int i, int i2, int i3) {
        this.mRangeValueResource = new LayoutResource(i, i2, i3);
    }

    public void setDateLayoutResourceId(int i, int i2, int i3) {
        this.mDateResource = new LayoutResource(i, i2, i3);
    }

    public void setDatePickerLayoutResourceId(int i) {
        this.mDatePickerResourceId = i;
    }

    public void setEditTextLayoutResourceId(int i, int i2, int i3) {
        this.mEditTextResource = new LayoutResource(i, i2, i3);
    }

    /* loaded from: classes.dex */
    protected class AttributeInfo {
        public Domain domain;
        public Field field;
        public PopupFieldInfo fieldInfo;
        public Object value;

        protected AttributeInfo() {
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public FIELD_TYPE determineFieldType(AttributeInfo attributeInfo) {
        ArcGISPopupInfo arcGISPopupInfo;
        if (attributeInfo.field == null) {
            return FIELD_TYPE.TEXT;
        }
        boolean z = true;
        if (this.mFeatureType != null) {
            if (!(this.mPopup.a instanceof ArcGISPopupInfo)) {
                arcGISPopupInfo = new ArcGISPopupInfo(this.mPopup.a);
            } else {
                arcGISPopupInfo = (ArcGISPopupInfo) this.mPopup.a;
            }
            String name = attributeInfo.field.getName();
            if (name != null && !name.equalsIgnoreCase(arcGISPopupInfo.getTypeIdField()) && (this.mFeatureType.getDomains() == null || this.mFeatureType.getDomains().isEmpty())) {
                z = false;
            }
        }
        int fieldType = attributeInfo.field.getFieldType();
        if (z && attributeInfo.domain != null && (attributeInfo.domain instanceof CodedValueDomain)) {
            return FIELD_TYPE.CODED_VALUE_DOMAIN;
        }
        if (z && attributeInfo.domain != null && (attributeInfo.domain instanceof RangeDomain) && (fieldType == 10 || fieldType == 20)) {
            return FIELD_TYPE.RANGE_DOMAIN;
        }
        return a(fieldType);
    }

    private FIELD_TYPE a(int i) {
        if (i != 50) {
            if (i == 70 || i == 80 || i == 90 || i == 100 || i == 110 || i == 120 || i == 130) {
                return FIELD_TYPE.UNSUPPORTED;
            }
            return FIELD_TYPE.TEXT;
        }
        return FIELD_TYPE.DATE;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public AttributeInfo getAttributeInfo(int i) {
        PopupInfo popupInfo = this.mPopup.a;
        AttributeInfo attributeInfo = new AttributeInfo();
        String attributeName = getAttributeName(i);
        if (popupInfo != null) {
            attributeInfo.fieldInfo = popupInfo.getFieldInfo(attributeName);
            if (popupInfo instanceof ArcGISPopupInfo) {
                attributeInfo.field = ((ArcGISPopupInfo) popupInfo).getField(attributeName);
                attributeInfo.domain = attributeInfo.fieldInfo == null ? null : this.mValueFormat.getDomain(this.mFeatureType, attributeInfo.fieldInfo, attributeInfo.field);
            }
        }
        attributeInfo.value = getAttributeValue(attributeName);
        return attributeInfo;
    }

    @Override // android.widget.Adapter
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {
        this.c.registerObserver(dataSetObserver);
    }

    @Override // android.widget.Adapter
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
        this.c.unregisterObserver(dataSetObserver);
    }

    public void notifyDataSetChanged() {
        this.c.notifyChanged();
    }

    public void notifyDataSetInvalidated() {
        this.c.notifyInvalidated();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public ArcGISAttributesAdapter(Context context, Popup popup) {
        this.mContext = context;
        this.mPopup = popup;
        this.mValueFormat = new ArcGISValueFormat(popup);
        reset();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public int a() {
        return PopupUtil.convertDpInPixels(this.mContext, this.b);
    }

    public void reset() {
        ArcGISPopupInfo arcGISPopupInfo;
        String typeIdField;
        LinkedHashMap linkedHashMap;
        if (this.mPopup.a != null && (linkedHashMap = (LinkedHashMap) this.mPopup.a.getFieldInfos()) != null && !linkedHashMap.isEmpty()) {
            this.a = new ArrayList<>(linkedHashMap.keySet());
        }
        if (this.mPopup.b != null && this.mPopup.b.getAttributes() != null) {
            if (this.a == null) {
                this.a = new ArrayList<>(this.mPopup.b.getAttributes().keySet());
            }
            if (this.a != null) {
                this.mAttributes = new LinkedHashMap(this.a.size());
                Iterator<String> it = this.a.iterator();
                while (it.hasNext()) {
                    String next = it.next();
                    this.mAttributes.put(next, this.mPopup.b.getAttributeValue(next));
                }
            } else {
                this.mAttributes = new LinkedHashMap(0);
            }
        }
        if (this.mPopup.a == null || !(this.mPopup.a instanceof ArcGISPopupInfo) || (typeIdField = (arcGISPopupInfo = (ArcGISPopupInfo) this.mPopup.a).getTypeIdField()) == null || typeIdField == "") {
            return;
        }
        this.mFeatureType = arcGISPopupInfo.getType(String.valueOf(getAttributeValue(typeIdField)));
    }

    @Override // android.widget.Adapter
    public boolean isEmpty() {
        return getCount() == 0;
    }

    @Override // android.widget.Adapter
    public int getCount() {
        ArrayList<String> arrayList = this.a;
        if (arrayList == null) {
            return 0;
        }
        return arrayList.size();
    }

    public String getAttributeName(int i) {
        if (i >= getCount()) {
            return null;
        }
        return this.a.get(i);
    }

    public Object getAttributeValue(String str) {
        Map<String, Object> map = this.mAttributes;
        if (map == null) {
            return null;
        }
        return map.get(str);
    }

    @Override // android.widget.Adapter
    public Object getItem(int i) {
        return getAttributeValue(getAttributeName(i));
    }

    /* loaded from: classes.dex */
    public static class FieldLabelView extends TextView implements ArcGISPopupStyle.IPopupStylable {
        public FieldLabelView(Context context, String str, ArcGISPopupStyle arcGISPopupStyle) {
            super(context);
            setPadding(0, PopupUtil.convertDpInPixels(context, 15), PopupUtil.convertDpInPixels(context, 10), PopupUtil.convertDpInPixels(context, 5));
            applyStyle(arcGISPopupStyle, ArcGISPopupStyle.STYLE_ELEMENT.FIELD_LABEL);
            setText(str);
            arcGISPopupStyle.registerStylable(this);
        }

        @Override // com.esri.android.map.popup.ArcGISPopupStyle.IPopupStylable
        public Set<ArcGISPopupStyle.STYLE_ELEMENT> getRegisteredStyle() {
            return EnumSet.of(ArcGISPopupStyle.STYLE_ELEMENT.FIELD_LABEL);
        }

        @Override // com.esri.android.map.popup.ArcGISPopupStyle.IPopupStylable
        public void applyStyle(ArcGISPopupStyle arcGISPopupStyle, ArcGISPopupStyle.STYLE_ELEMENT style_element) {
            setTextSize(1, arcGISPopupStyle.getFieldLabelFontSize());
            setTypeface(arcGISPopupStyle.getFieldLabelFontType(), arcGISPopupStyle.getFieldLabelFontStyle());
            setTextColor(arcGISPopupStyle.getFieldLabelColor());
        }
    }

    /* loaded from: classes.dex */
    public static class SeparatorView extends View implements ArcGISPopupStyle.IPopupStylable {
        public SeparatorView(Context context, ArcGISPopupStyle arcGISPopupStyle) {
            super(context);
            applyStyle(arcGISPopupStyle, ArcGISPopupStyle.STYLE_ELEMENT.SEPARATOR);
            arcGISPopupStyle.registerStylable(this);
        }

        @Override // com.esri.android.map.popup.ArcGISPopupStyle.IPopupStylable
        public Set<ArcGISPopupStyle.STYLE_ELEMENT> getRegisteredStyle() {
            return EnumSet.of(ArcGISPopupStyle.STYLE_ELEMENT.SEPARATOR);
        }

        @Override // com.esri.android.map.popup.ArcGISPopupStyle.IPopupStylable
        public void applyStyle(ArcGISPopupStyle arcGISPopupStyle, ArcGISPopupStyle.STYLE_ELEMENT style_element) {
            setVisibility(arcGISPopupStyle.isShowFieldSeparator() ? 0 : 8);
            if (arcGISPopupStyle.getCustomFieldSeparator() != null) {
                arcGISPopupStyle.unregisterStylable(this);
                ViewGroup viewGroup = (ViewGroup) getParent();
                if (viewGroup != null) {
                    int indexOfChild = viewGroup.indexOfChild(this);
                    viewGroup.removeView(this);
                    viewGroup.addView(arcGISPopupStyle.getCustomFieldSeparator(), indexOfChild);
                    viewGroup.postInvalidate();
                    return;
                }
                return;
            }
            setBackgroundColor(arcGISPopupStyle.getFieldSeparatorColor());
            setLayoutParams(new LinearLayout.LayoutParams(-1, arcGISPopupStyle.getFieldSeparatorWidth()));
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Map<String, Object> b() {
        ArcGISPopupInfo arcGISPopupInfo;
        PopupFieldInfo fieldInfo;
        HashMap hashMap = new HashMap();
        if (!(this.mPopup.a instanceof ArcGISPopupInfo)) {
            arcGISPopupInfo = new ArcGISPopupInfo(this.mPopup.a);
        } else {
            arcGISPopupInfo = (ArcGISPopupInfo) this.mPopup.a;
        }
        for (String str : this.mAttributes.keySet()) {
            Field field = arcGISPopupInfo.getField(str);
            if (field != null && a(field.getFieldType()) != FIELD_TYPE.UNSUPPORTED && (fieldInfo = arcGISPopupInfo.getFieldInfo(str)) != null && fieldInfo.isEditable() && fieldInfo.isVisible()) {
                Object obj = this.mAttributes.get(str);
                Object attributeValue = this.mPopup.b.getAttributeValue(str);
                if ((obj != null && !obj.equals(attributeValue)) || (obj == null && arcGISPopupInfo.getField(str) != null && arcGISPopupInfo.getField(str).isNullable())) {
                    hashMap.put(str, obj);
                }
            }
        }
        return hashMap;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean c() {
        return !b().isEmpty();
    }
}
